package com.gxa.modules.sys.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.utils.PageUtils;
import com.gxa.common.utils.Result;
import com.gxa.common.validator.ValidatorUtils;
import com.gxa.common.validator.group.UpdateGroup;
import com.gxa.modules.sys.dto.ClockLeaveDto;
import com.gxa.modules.sys.dto.ClockRankDto;
import com.gxa.modules.sys.dto.SignInDto;
import com.gxa.modules.sys.entity.Clock;
import com.gxa.modules.sys.entity.User;
import com.gxa.modules.sys.service.ClockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(tags = "打卡相关接口")
@RestController
@Slf4j
public class ClockController {

    @Autowired
    private ClockService clockService;

    //@RequiresPermissions("sys:clock:list")
    @ApiOperation(value="后台打卡记录分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "page",value ="当前是第几页",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "limit",value ="每页显示多少条",dataType ="int"),
            @ApiImplicitParam(paramType = "query",required = true,name = "userId",value ="必传：用户id",dataType ="int"),
            @ApiImplicitParam(paramType = "query",name = "year",value ="查询条件：年份",dataType ="String"),
            @ApiImplicitParam(paramType = "query",name = "month",value ="查询条件：月份",dataType ="String"),
    })
    @GetMapping("/clock/list")
    public Result list(@RequestParam @ApiIgnore Map<String,Object> params){

        log.info("----params-----{}----",params);

        Page<Map<String,Object>> pageHelperList = PageHelper.startPage(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString()));

        if (params.get("userId") == null)
        {
            return new Result<>().error("userId不能为空！");
        }

        List<Clock> clocks = this.clockService.queryByPageList(params);
        Map map = new HashMap();
        map.put("clocks",clocks);
        map.put("count",pageHelperList.getTotal());

        return new Result().ok(map);
    }

    //@RequiresPermissions("sys:clock:leave")
    @ApiOperation(value = "确认离店接口")
    @PutMapping("/clock/confirmLeave")
    public Result confirmLeave(@RequestBody ClockLeaveDto params) {
        System.out.println("-------->"+ params.toString());

        this.clockService.updateMyOrderStateByOrderId(params);
        this.clockService.updateSeatStateByOrderId(params);
        this.clockService.updateOrderSeatStateByOrderId(params);
        this.clockService.updateClockEndTimeByEndTime(params);

        return new Result();
    }

    //@RequiresPermissions("sys:clock:sign")
    @ApiOperation(value="前端打卡详情查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",required = true,name = "userId",value ="用户id",dataType ="int"),
    })
    @GetMapping("/clock/signIn")
    public Result signIn(@RequestParam @ApiIgnore Integer userId){

        System.out.println("-------->"+ userId);

        SignInDto signInDto = this.clockService.queryClockByUserId(userId);

        return new Result<>().ok(signInDto);
    }

    //@RequiresPermissions("sys:clock:open")
    @ApiOperation(value="打卡开门接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",required = true,name = "userId",value ="用户id",dataType ="int"),
    })
    @GetMapping("/clock/open")
    public Result openDoor(@RequestParam @ApiIgnore Integer userId){

        System.out.println("-------->"+ userId);

        this.clockService.addClock(userId);
        this.clockService.updateSeatStateByUserId(userId);
        this.clockService.updateMyOrderStateByUserId(userId);

        return new Result();
    }

    //@RequiresPermissions("sys:clock:rank")
    @ApiOperation(value="前端打卡排行总榜查询接口")
    @GetMapping("/clock/rank")
    public Result rank(){

        List<ClockRankDto> clockRankDto = this.clockService.queryClockRank();

        return new Result<>().ok(clockRankDto);
    }

    //@RequiresPermissions("sys:clock:userRank")
    @ApiOperation(value="前端打卡个人排行查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",required = true,name = "userId",value ="用户id",dataType ="int"),
    })
    @GetMapping("/clock/userRank")
    public Result userRank(@RequestParam @ApiIgnore Integer userId){

        ClockRankDto clockRankDto = this.clockService.queryClockRankByUserId(userId);

        return new Result<>().ok(clockRankDto);
    }
}
